package tp.pr5.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import tp.pr5.RobotEngineObserver;
import tp.pr5.items.InventoryObserver;
import tp.pr5.items.Item;

public class RobotPanel extends JPanel implements RobotEngineObserver, InventoryObserver
{
	private static final long serialVersionUID = 1L;
	
	private JPanel robotInfo;
	private JPanel robotStat;
	
	private JScrollPane scrollPane;
	private JTable table;
	
	private JLabel fuelLabel;
	private JLabel materialLabel;

	/**
	 * Main Constructor
	 */
	public RobotPanel()
	{
		super();
		robotInfo = new JPanel();
		robotStat = new JPanel();
		scrollPane = new JScrollPane(table);
		fuelLabel = new JLabel("Fuel: 100");
		materialLabel = new JLabel("Recycled: 0");
		table = new JTable();
		build();
	}
	
	/**
	 * Builds the user interface
	 */
	
	public void build()
	{
		this.setLayout(new BorderLayout());
		this.add(robotInfo, BorderLayout.CENTER);
		
		robotInfo.setLayout(new BorderLayout());
		robotInfo.setBorder(BorderFactory.createTitledBorder("Robot info"));
		robotInfo.add(robotStat, BorderLayout.NORTH);
		robotInfo.add(scrollPane, BorderLayout.CENTER);
		
		robotStat.add(fuelLabel);
		robotStat.add(materialLabel);
		
		String[][] datos = new String[0][0];
		table.setModel(new Items(datos));
	    table.setPreferredScrollableViewportSize(new Dimension(0, 80));
	    
	    scrollPane.setViewportView(table);
	}
	
	/**
	 * Select the focused item
	 * 
	 * @return the item that is focused. If the focus is empty it returns null
	 */
	
	public String getSelectedItem()
	{
		int position = this.table.getSelectedRow();
		
		if(position > -1)
		{
			return this.table.getValueAt(position,0).toString();
		}
		return null;
	}
	
	/**
	 * Builds the table
	 */
	
	class Items extends AbstractTableModel
	{
		private static final long serialVersionUID = 1L;
		
		public String[] columns = {"Id","Description"};
		public String[][] content;
		
		public Items(String[][] content)
		{
			this.content = content;
		}
		@Override
		public int getRowCount() {
			return content.length;
		}

		@Override
		public int getColumnCount() {
			return columns.length;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return content[rowIndex][columnIndex];
		}
		@Override
		public String getColumnName(int IndexCol) {
			return columns[IndexCol];
		}
	}
	
	/**
	 * Sets the JTable with the table given
	 * 
	 * @param table
	 */
	
	public void updateTable(String table[][])
	{
		this.table.setModel(new Items(table));
	}

	/**
	 * Notifies that the container has changed
	 * 
	 * @param inventory - New inventory
	 */
	@Override
	public void inventoryChange(List<Item> inventory)
	{
		String[][] items = new String[inventory.size()][2];
		
		for (int i = 0; i < inventory.size(); i++)
		{
			items[i][0] = inventory.get(i).getId();
			items[i][1] = inventory.get(i).toString();
		}
		
		this.updateTable(items);
	}

	/**
	 * Notifies that the user requests a SCAN instruction over the inventory.
	 * 
	 * @param collectionDescription - Information about the inventory
	 */
	@Override
	public void inventoryScanned(String inventoryDescription) 
	{

	}

	/**
	 * Notifies that an item is empty and it will be removed from the container. An invocation to the inventoryChange method will follow.
	 * 
	 * @param itemName - Name of the empty item
	 */
	@Override
	public void itemEmpty(String itemName) 
	{

	}

	/**
	 * Notifies that the user wants to scan an item allocated in the inventory
	 * 
	 * @param description - Item description
	 */
	@Override
	public void itemScanned(String description)
	{
	
	}

	/**
	 * The robot engine informs that it has raised an error
	 * 
	 * @param msg - Error message
	 */
	@Override
	public void raiseError(String msg) 
	{
	
	}

	/**
	 * The robot engine informs that the help has been requested
	 * 
	 * @param help - A string with information help
	 */
	@Override
	public void communicationHelp(String help)
	{

	}

	/**
	 * The robot engine informs that the robot has shut down (because it has arrived at the spaceship or it has run out of fuel)
	 * 
	 * @param atShip - true if the robot shuts down because it has arrived at the spaceship or false if it has run out of fuel
	 */
	@Override
	public void engineOff(boolean atShip)
	{
		
	}

	/**
	 * The robot engine informs that the communication is over.
	 * 
	 */
	@Override
	public void communicationCompleted() 
	{
		
	}

	/**
	 * The robot engine informs that the fuel and/or the amount of recycled material has changed
	 * 
	 * @param fuel - Current amount of fuel
	 * @param recycledMaterial - Current amount of recycled material
	 */
	@Override
	public void robotUpdate(int fuel, int recycledMaterial) 
	{
		this.fuelLabel.setText("Fuel: " + fuel);
		this.materialLabel.setText("Recycled: " + recycledMaterial);
	}

	/**
	 * The robot engine informs that the robot wants to say something
	 * 
	 * @param message - The robot message
	 */
	@Override
	public void robotSays(String message) 
	{
		
	}
	
}
